let app = getApp();
Page({

  ...app.globalData.function,
  data: {
    ...app.globalData.config,
    order_list:"",
    fileList:[],
    options:{},
    refund_remark:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that=this
    this.orderInfo(options).then(res=>{               
      if(res.resCode=="9999"){
        that.setData({
          order_list:res.data,
          options
        })
      }else{
        wx.showToast({title:res.resMessage,icon:"none"})
      }
    })

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  //提交申请退款
  dosubmitok(){   
    let options=this.data.options
    options.refund_remark=this.data.refund_remark
    if(!this.data.refund_remark){
      this.$showToast('请输入退款说明！')
      return
    }
    options.image=JSON.stringify(this.data.fileList)  
    options.type=1  
    // console.log(options)
    this.orderBack(options).then(res=>{
      if(res.resCode=='9999'){
        this.$showToast(res.resMessage) 
        setTimeout(()=>{
          this.router('/pages/order/index')        
        },3000) 
      }
     
    })

  },

  afterRead(event) {
    const { file } = event.detail;    
    var that = this
    const fileList = that.data.fileList
    //获得这次上传的图片数量，上传时避免重复上传之前传过的文件
    const thisNum = file.length 
    const beforeNum = fileList.length 
    const totalNum = thisNum + beforeNum
    //还没上传时将选择的图片的上传状态设置为加载    
    for (let j = 0; j < thisNum; j++) {
      file[j].status='uploading'
      fileList.push(file[j])
    }
    that.setData({ fileList })
    // console.log('fileList',that.data.fileList)
    // console.log('file',file)
    //上传服务器
    for (let i = beforeNum; i < totalNum; i++) {
          that.uploadImg(i,that.data.fileList[i].url )
    }
  },
  uploadImg(fileListIndex,fileURL) {
    var that = this
    //上传文件
    const filePath = fileURL // 小程序临时文件路径
    // console.log("filePath",filePath)
    wx.uploadFile({
      url: this.data.api_url+'/api/index/uploadImage',
      filePath: filePath,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data",
      },
      formData: { 
        fileType:'image',
        reName:'true',
        thumbnail:'true'
      },
      success(res) {
      var tem = JSON.parse(res.data)
      // 上传完成需要更新 fileList
      that.setData({
        [`fileList[${fileListIndex}].url`]: tem.data.image,
        [`fileList[${fileListIndex}].status`]: 'done'
      })
      wx.showToast({ title: '上传成功', icon: 'none' })

      },
      fail: function (res) {
        console.log("file upload fail")
      },
    })
  },

   /* 删除图片*/
   deleteImg(event) {
    const delIndex = event.detail.index
    const { fileList } = this.data
    fileList.splice(delIndex, 1)
    this.setData({
      fileList
    })
  },
  
})